package com.mifengkong.kotlincommon.indicator

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.view.animation.DecelerateInterpolator
import android.widget.FrameLayout
import android.widget.LinearLayout
import com.mifengkong.kotlincommon.dp

/**
 * Created by jack on 2018/5/31
 */
class IndicatorViewGroup : FrameLayout {

    private var mIndicatorGroup: LinearLayout  //Item的容器
    private var mItemWidth: Int = 0
    private var mBottomTrackView: View? = null
    private lateinit var mParams: LayoutParams
    private var mInitLeftMargin: Int = 0

    private var DEFAULT_BOTTOM_VIEW_HEIGHT: Int = 2.dp()

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attr: AttributeSet?) : this(context, attr, 0)

    constructor(context: Context, attr: AttributeSet?, defStyleAttr: Int) : super(context, attr, defStyleAttr) {
        mIndicatorGroup = LinearLayout(context)
        addView(mIndicatorGroup)
    }

    fun addItemView(view: View) {
        mIndicatorGroup.addView(view)
    }

    fun getItemViewAt(position: Int): View {
        return mIndicatorGroup.getChildAt(position)
    }

    /**
     * 设置底部追踪器（通常是下划线）
     */
    fun setBottomTrackView(bottomTrackView: View?, itemWidth: Int) {
        if (null == bottomTrackView) {
            return
        }
        mItemWidth = itemWidth
        mBottomTrackView = bottomTrackView
        //添加底部的追踪器（通常是下划线）
        addView(mBottomTrackView)
        mParams = mBottomTrackView?.layoutParams as LayoutParams
        mParams.gravity = Gravity.BOTTOM
        var trackViewWidth = mParams.width
        if (mParams.width == LayoutParams.MATCH_PARENT) {
            trackViewWidth = mItemWidth
        }
        if (trackViewWidth > mItemWidth) {
            trackViewWidth = mItemWidth
        }
        mParams.width = trackViewWidth

        //确保追踪器（通常是下划线）处于item的中间
        mParams.leftMargin = (mItemWidth - trackViewWidth) / 2
        mInitLeftMargin = mParams.leftMargin
        mParams.leftMargin = mInitLeftMargin
    }

    /**
     * 让底部追踪器（通常是下划线）移动到当前指示器下：滑动时
     */
    fun scrollBottomTrackView(position: Int, positionOffset: Float) {
        if (null == mBottomTrackView) {
            return
        }
        val leftMargin: Int = ((position + positionOffset) * mItemWidth).toInt()
        mParams.leftMargin = leftMargin + mInitLeftMargin
        mBottomTrackView?.layoutParams = mParams
    }

    /**
     * 让底部追踪器（通常是下划线）移动到当前指示器下：点击时
     */
    fun scrollBottomTrackView(position: Int) {
        if (null == mBottomTrackView) {
            return
        }
        val finalMargin = position * mItemWidth + mInitLeftMargin   //最终要移动的位置
        val currentMargin = mParams.leftMargin   //当前位置
        //需要移动的距离
        val distance = finalMargin - currentMargin
        val animator: ValueAnimator = ObjectAnimator.ofFloat(currentMargin.toFloat(), finalMargin.toFloat())
                .setDuration((Math.abs(distance) * 0.4).toLong())
        animator.addUpdateListener { animation ->
            val currentLeftMargin: Float = animation.animatedValue as Float
            mParams.leftMargin = currentLeftMargin.toInt()
            mBottomTrackView?.layoutParams = mParams
        }
        //设置一个减速的差值器
        animator.interpolator = DecelerateInterpolator()
        animator.start()
    }

    fun requestViewLayout() {
        mIndicatorGroup.requestLayout()
    }
}